package net.moao.netutils_library.tcp;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 * TCPClient
 *
 * @author Moao.Net
 * @DATE 2024/6/10
 */
public class TCPClient {
    private final String TAG = "TCPClient";
    Socket client;
    InputStream inputStream;
    OutputStream outputStream;
    public boolean isConnTCP = false;


    public boolean connect(String ip, Integer port, int soTimeout) {
        try{
            client = new Socket();
            client.connect(new InetSocketAddress(ip,port),1500);
            client.setSoTimeout(soTimeout);
            isConnTCP = true;
        }catch (Exception e){
            Log.e(TAG, "connect: 未知原因，e : ", e);
            isConnTCP = false;
        }
        return isConnTCP;
    }

    public void closed() {
        try{
            client.close();
            isConnTCP = false;
        }catch (Exception e){
            Log.e(TAG, "closed: e : ", e);
            e.printStackTrace();
        }
    }

    public byte[] receive(){
        byte[] strRecover;
        if (isConnTCP){
            try {
                inputStream = client.getInputStream();
                byte[] data = new byte[1024];
                int len = inputStream.read(data);
                if (len == -1){
                    // 目前一般上是由于断网造成的
                    client.close();
                    isConnTCP = false;
                }
                strRecover = new byte[len];
                System.arraycopy(data, 0, strRecover, 0, len);
            }catch (SocketTimeoutException timeoutException){
//                Log.e(TAG, "receive: 接收超时，E: ", timeoutException);
                // 超时了，重新即可
                strRecover = timeoutException.toString().getBytes();
            }
            catch (Exception e){
                Log.e(TAG, "receive: 未知原因，E: ", e);
                // 超时了，重新即可
                strRecover = e.toString().getBytes();
            }
        }else {
            strRecover = "java.net.SocketTimeoutException:".getBytes();
        }
        return strRecover;
    }

    public String send(String str){
        String strSend = "";
        if (isConnTCP){
            try {
                outputStream = client.getOutputStream();
                outputStream.write(str.getBytes());
                strSend = "发送成功!";
            }catch (Exception e){
                Log.e(TAG, "send: 未知原因，e :", e);
                strSend = e.toString();
            }
        }else {
            strSend = "未连接!";
        }
        return strSend;
    }

    public String sendHexMessage(byte[] message){
        String strSend = "";
        if (isConnTCP) {
            try{
                outputStream = client.getOutputStream();
                if (outputStream != null) {
                    outputStream.write(message);
                    outputStream.flush();
                    strSend = "发送成功";
                }
            }catch (IOException e){
                strSend = e.toString();
            }
        }else {
            strSend = "未连接!";
        }
        return strSend;
    }
}
